import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from "element-plus";


const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect:'/index',
    children:[
      {path: '/index', component: () => import('../views/index/IndexView.vue')},
      {path: '/doctor', component: () => import('../views/doctor/DoctorView.vue')},
      {path: '/record', component: () => import('../views/record/RecordView.vue')},

      {path: '/users', component: () => import('../views/users/UserView.vue')},
      {path: '/feedback', component: () => import('../views/users/FeedbackView.vue')},
      {path: '/drugs', component: () => import('../views/drugs/DrugsView.vue')},
      {path: '/order', component: () => import('../views/order/OrderView.vue')},
      {path: '/RechargeOrder', component: () => import('../views/order/RechargeOrderView.vue')},

      {path: '/dict', component: () => import('../views/dict/DictView.vue')},
      {path: '/dictOption', component: () => import('../views/dict/DictOptionView.vue')},



    ]
  },
  {path: '/login', component: () => import('../views/LoginView.vue')}
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  //如果访问登录页面   或  注册页面
  if (to.path == '/login' || to.path == "/register") {
    next();//放行
  }else{//其他页面都需要判断你是否登录过
    //获取localStorage的登录信息
    //有风险：redis有30分钟，localStorage一致存在 - 注销的时候一定要删除localStorage的数据
    let token = localStorage.getItem('token');
    if (token) {
      next(); //登录过放行
    } else {
      next({path: '/login'});//跳转到login - 登录页面
    }
  }
});
export default router
